var token;
var u_id;
var num;
var grade;
var i_id;

var courseList_size;


//接收多值的js函数
function manyValues() {
    var url = window.location.search;
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        key = [token, u_id, num, i_id];
        var value = new Array(strs.length);
        for (i = 0; i < strs.length; i++) {
            value[i] = unescape(strs[i].split("=")[1]);
            key[i] = value[i];
        }
    }
}

//接收Login.html传过来的值
manyValues();
token = key[0];
u_id = key[1];
num = key[2];
i_id = key[3];
//获取年级
getGrade();

//查询管理员的班级
function getGrade() {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        type: "GET",
        url: "/api/user/getGradeByNum?num=" + num,
        async: false,
        success: function (result) {
            grade = result.data;
        }
    });
}

listCourse();

//查询课程
function listCourse() {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        contentType: 'application/json',
        type: "POST",
        url: "/api/course/listCourseByInstitutionGrade",
        data: JSON.stringify({
            "institutionId": i_id,
            "grade": grade,
        }),
        async: false,
        success: function (result) {
            var txt = "";
            var list = result.data;
            courseList_size = list.length;
            for (var i = 0; i < list.length; i++) {
                var txt2 = '    <div>\n' +
                    '        <p class="courseId" style="display: none">' + list[i].courseId + '</p>\n' +
                    '        <p class="courseName">课程名：' + list[i].courseName + '</p>\n' +
                    '        <p class="teacherId" style="display: none"></p>\n' +
                    '        <p class="teacherName">教师名称：' + list[i].teacherName + '</p>\n' +
                    '        <p class="institutionName">隶属机构：' + list[i].institutionName + '</p>\n' +
                    '        <p class="clazzNameList">上课班级：' + list[i].clazzNameList + '</p>\n' +
                    '        <p><button class="btn-lookExam" value=' + list[i].courseId + '>查看考试安排</button></p>\n' +
                    '        <p><button class="btn-arrangeExam" value=' + list[i].courseId + '>安排考试</button></p>\n' +
                    '    </div>'
                txt += txt2;
            }
            $("#div-courseList").html(txt);
        }
    });
}

//安排课程
function arrangeExamByCourseId(courseId) {
    listTeachers();
    listBank();
    //点击确认按钮
    $("#btn-arrange").click(function () {
        $.ajax({
            headers: {
                'Authorization': token,
            },
            contentType: 'application/json; charset=utf-8',
            type: "POST",
            url: "/api/examination/arrangeExamByCourseId",
            data: JSON.stringify({
                "courseId": courseId,
                "examName": $("#examName").val(),
                "invigilatorId": $("#choice-teacher option:selected").val(),
                "startTime": $("#startTime").val(),
                "duration": $("#choice-time option:selected").val(),
                "bankId": $("#choice-bank option:selected").val(),
            }),
            async: false,
            success: function (result) {
                alert(result.message);
                location.reload();
            }
        });
    });

}

//查询监考老师
function listTeachers() {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        type: "GET",
        url: "/api/user/listInvigilatorByInstitutionId?institutionId=" + i_id,
        async: false,
        success: function (result) {
            var txt = "";
            var list = result.data;
            for (var i = 0; i < list.length; i++) {
                var txt2 = '<option value=' + list[i].id + '>' + list[i].name + list[i].num + '</option>'
                txt += txt2;
            }
            $("#choice-teacher").html(txt);
        }
    });
}

//查询题库
function listBank() {
    $.ajax({
        headers: {
            'Authorization': token,
        },
        contentType: 'application/json; charset=utf-8',
        type: "POST",
        url: "/api/examination/listBankByInstitutionGrade",
        data: JSON.stringify({
            "institutionId": i_id,
            "grade": grade,
        }),
        async: false,
        success: function (result) {
            var txt = "";
            var list = result.data;
            for (var i = 0; i < list.length; i++) {
                var txt2 = '<option value=' + list[i].bankId + '>' + list[i].bankName + '</option>'
                txt += txt2;
            }
            $("#choice-bank").html(txt);
        }
    });
}

for (let i = 0; i < courseList_size; i++) {
    //安排考试按钮
    $($(".btn-arrangeExam")[i]).click(function () {
        // $(".div-arrangeExamByCourseId").style.display="block";
        $(".span-course-id").html($($(".btn-arrangeExam")[i]).val());
        $("#nowChoice-course").html($($(".courseName")[i]).html())
        arrangeExamByCourseId($($(".btn-arrangeExam")[i]).val())
    });

    //查看考试安排按钮
    $($(".btn-lookExam")[i]).click(function () {
        $.ajax({
            headers: {
                'Authorization': token,
            },
            type: "GET",
            url: "/api/examination/getExamByCourseId?courseId=" + $($(".courseId")[i]).html(),
            async: false,
            success: function (result) {
                if (result.success) {
                    alert(result.data.examName + '\n' +
                        "开始时间：" + result.data.startTime + '\n' +
                        "结束时间：" + result.data.endTime + '\n' +
                        "状态：" + result.data.statusName);
                } else {
                    alert(result.message)
                }

            }
        });
    });
}

//跳转到添加题库页面
$("#btn-addBank").click(function () {
    window.location.href = '/api/templates/AddBank.html?token=' + token + "&u_id=" + u_id + "&num=" + num + "&i_id=" + i_id
});
